<?php
/**
 * File description.
 * 
 * @filesource
 * @author Michael N <webjestic@gmail.com>
 * @link http://webjestic.net webJestic
 * @license http://www.opensource.org/licenses/bsd-license.php BSD
 * @copyright Copyright (c) 2009 - webJestic.NET.  All Rights Reserved.
 * 
 * @category   webJestic
 * @package    Application 
 * @subpackage Index
 * @version    $Id$
 */


class Application_Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

	public function _initApplication() {
		require_once 'Engine/Functions.php';
		
		// Save basrUrl to Registry
        if (isset($_SERVER['HTTPS'])) {
        	Zend_Registry::set('baseUrl', 'https://'.$_SERVER['HTTP_HOST']);
        } else {
            Zend_Registry::set('baseUrl', 'http://'.$_SERVER['HTTP_HOST']);
        }  		
		
		// Force Resources to load in desired order - Order is Important
		$this->bootstrap('database');
		$this->bootstrap('cache');
		$this->loadEngineConfig();	
		$this->bootstrap('log');
		$this->bootstrap('session');	
		$this->bootstrap('locale');	
		$this->bootstrap('auth');
		$this->bootstrap('front');
	}
	
	
	public function loadEngineConfig() {
		$cache = $this->getResource('cache');
		if( ($config = $cache->load('core_config')) === false ) {			
			$core_config = new Engine_Db_Table_CoreConfig();
			$results = $core_config->fetchAll();			
			foreach ($results as $row) {
				$config[$row['config_key']] = $row['config_value'];
			}
			$cache->save($config, 'core_config');
		}
		Zend_Registry::set('Core_Config', $config);	
		
		define('SITE_SALT', 		$config['site_salt']);
		define('SITE_NAME', 		$config['site_name']);
		define('FORMAT_DATETIME', 	$config['format_datetime']);
		define('FORMAT_DATE', 		$config['format_date']);
		define('FORMAT_TIME', 		$config['format_time']);
		
		date_default_timezone_set($config['site_timezone']);
	}
	
	
	public function _initModuleLoader() {	
		// Should already be cached from within the FrontController resource
		$cache = $this->getResource('cache');
		if( ($results = $cache->load('core_modules')) === false ) {
			
			$core_modules = new Engine_Db_Table_CoreModules();
			$select = $core_modules->select()->where('module_active = ?', true);
			$results = $core_modules->fetchAll($select);
			
			$cache->save($results, 'core_modules');
		}
		foreach($results as $row) {
			$module = new Zend_Application_Module_Autoloader(
						array (
							'namespace' => ucfirst($row['module_name']),
							'basePath' => APPLICATION_PATH . "/modules/". $row['module_name']
						)
			);
		}
	}
	
	
	public function _initWebjestic() {
		$webJestic = new Zend_Loader_Autoloader_Resource(
						array('basePath'  => INDEX_PATH.'/library/webJestic',
    							'namespace' => 'WJ',
		));
		$webJestic->addResourceType('plugin', 'Plugins/', 'Plugin')
              	 	->addResourceType('helper', 'Helpers/', 'Helper')
              	 	->addResourceType('widget', 'Widgets/', 'Widget')
              	 	->addResourceType('viewHelper', 'View/Helpers/', 'View_Helper')
              	 	->addResourceType('viewFilter', 'View/Filters/', 'View_Filter');

	}

	public function _initView() {		
		// Initialize view and assign XHTML Transitional (engine standard)
		$view = new Engine_View();
		$view->doctype('XHTML1_TRANSITIONAL');
		$view->setEncoding('UTF-8');

        // Add it to the ViewRenderer
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
        $viewRenderer->setView($view);
        
        $view->addHelperPath(INDEX_PATH."/library/webJestic/view/helpers", 'WJ_View_Helper');
        $view->addFilterPath(INDEX_PATH."/library/webJestic/view/filters", 'WJ_View_Filter');
        
        // Return it, so that it can be stored by the bootstrap
        return $view;		
	}
	
	
	public function _initRoutes() {
		
		// Retrieve the active routes from cache or the database
		$cache = $this->getResource('cache');
		if( ($results = $cache->load('core_routes')) === false ) {		
			$core_routes = new Engine_Db_Table_CoreRoutes();
			$select = $core_routes->select(Zend_Db_Table::SELECT_WITH_FROM_PART);
			$select->setIntegrityCheck(false);
			$select->joinLeft(CORE_MODULES, 'route_module_id = module_id');
			$select->where('module_active = TRUE and route_active = TRUE');
			$results = $core_routes->fetchAll($select);
			$cache->save($results, 'core_routes');
		}

		// Get the router from the front controller
		$front  = Zend_Controller_Front::getInstance();
		$router = $front->getRouter();		
		
		// Ass the active routes to the Router
		foreach ($results as $record) {
			$router->addRoute($record["route_name"],
									new Zend_Controller_Router_Route(
										$record["route_uri"],
										array(	'module' 		=> $record["module_name"],
												'controller' 	=> $record["route_controller"],
												'action' 		=> $record["route_action"]
										)
							 		)
			);
		}		
	}
	
	
	public function _initMobile() {
		// Creat the Mobile session namespace
		$mobileSession = new Zend_Session_Namespace('Mobile');
		$mobileSession->isMobile = false;
		
		if (preg_match('/iPhone|Android|BlackBerry|iPad/', $_SERVER['HTTP_USER_AGENT'])) {
			$mobileSession->isMobile = true;	
		}
	}

	
}

?>